package com.grace.web.controller.cms;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.oly.cms.admin.service.ICmsCategoryService;
import com.oly.cms.common.domain.entity.CmsCategory;
import com.grace.common.annotation.Log;
import com.grace.common.core.controller.BaseController;
import com.grace.common.core.domain.AjaxResult;
import com.grace.common.enums.BusinessType;
import com.grace.common.utils.poi.ExcelUtil;

/**
 * 分类Controller
 * 
 * @author zhige
 * @date 2022-11-18
 */
@RestController
@RequestMapping("/cms/category")
public class CmsCategoryController extends BaseController {
    @Autowired
    private ICmsCategoryService cmsCatService;

    /**
     * 查询分类列表
     */
    @PreAuthorize("@ss.hasPermi('cms:cat:list')")
    @GetMapping("/list")
    public AjaxResult list(CmsCategory cmsCat) {
        List<CmsCategory> list = cmsCatService.listCmsCatByCat(cmsCat);
        return success(list);
    }

    /**
     * 导出分类列表
     */
    @PreAuthorize("@ss.hasPermi('cms:cat:export')")
    @Log(title = "分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmsCategory cmsCat) {
        List<CmsCategory> list = cmsCatService.listCmsCatByCat(cmsCat);
        ExcelUtil<CmsCategory> util = new ExcelUtil<CmsCategory>(CmsCategory.class);
        util.exportExcel(response, list, "分类数据");
    }

    /**
     * 获取分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('cms:cat:query')")
    @GetMapping(value = "/{catId}")
    public AjaxResult getInfo(@PathVariable("catId") Long catId) {
        return success(cmsCatService.selectCmsCatById(catId));
    }

    /**
     * 新增分类
     */
    @PreAuthorize("@ss.hasPermi('cms:cat:add')")
    @Log(title = "分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody CmsCategory cmsCat) {
        cmsCat.setCreateBy(getUsername());
        return toAjax(cmsCatService.insertCmsCat(cmsCat));
    }

    /**
     * 修改分类
     */
    @PreAuthorize("@ss.hasPermi('cms:cat:edit')")
    @Log(title = "分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody CmsCategory cmsCat) {
        cmsCat.setUpdateBy(getUsername());
        return toAjax(cmsCatService.updateCmsCat(cmsCat));
    }

    /**
     * 删除分类
     */
    @PreAuthorize("@ss.hasPermi('cms:cat:remove')")
    @Log(title = "分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{catId}")
    public AjaxResult remove(@PathVariable Long catId) {
        return toAjax(cmsCatService.deleteCmsCatById(catId));
    }
}
